<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../jquery.min.js"></script>
<script type="text/javascript" src="../jquery.easyui.min.js"></script>
<script type="text/javascript" src="../src/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../src/xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="../src/xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
<table id="news-datagrid" toolbar="#news-toolbar"></table>

<script type="text/javascript">
	$('#news-datagrid').datagrid({//加载表单数据
	    url:'http://localhost:3000/news/list',
	    fit:true,
	    loadFilter:pagerFilter,		
		rownumbers:true,
		singleSelect:false,
		pageSize:10,           
		pagination:true,
		multiSort:true,
		fitColumns:true,
	    columns:[[
	        {field:'title',title:'标题',width:100},
	        {field:'date',title:'日期',width:100,align:'right'},
	        {field:'operate',title:'管理', width:120,
			formatter: function(value,row,index){
				return '<a href="javascript:void(0)" name="search" class="easyui-linkbutton" onclick="findNews(\''+ row._id+'\')" >'+'<a href="javascript:void(0)" name="edit" class="easyui-linkbutton" onclick="editNews(\''+ row._id+'\')">'+
				 '<a href="javascript:void(0)" name="dele" class="easyui-linkbutton" onclick="deleNews(\''+ row._id+'\')">';
			}
		}
	    ]],
	    onLoadSuccess:function(data){
	       $("a[name='search']").linkbutton({text:'查看',plain:true,iconCls:'icon-search'});
           $("a[name='edit']").linkbutton({text:'修改',plain:true,iconCls:'icon-edit'});
           $("a[name='dele']").linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
        }
	});
	function pagerFilter(data){  //分页按钮插件          
		if (typeof data.length == 'number' && typeof data.splice == 'function'){// is array                
			data = {                   
				total: data.length,                   
				rows: data               
			}            
		}        
		var dg = $(this);         
		var opts = dg.datagrid('options');          
		var pager = dg.datagrid('getPager');          
		pager.pagination({                
			onSelectPage:function(pageNum, pageSize){                 
				opts.pageNumber = pageNum;                   
				opts.pageSize = pageSize;                
				pager.pagination('refresh',{pageNumber:pageNum,pageSize:pageSize});                  
				dg.datagrid('loadData',data);                
			}          
		});           
		if (!data.originalRows){               
			data.originalRows = (data.rows);       
		}         
		var start = (opts.pageNumber-1)*parseInt(opts.pageSize);          
		var end = start + parseInt(opts.pageSize);        
		data.rows = (data.originalRows.slice(start, end));         
		return data;       
	}
	
	function findNews(id){//点击查看相对应的文章模块级评论
		addTab('新闻评论','../a_news/comment/index.html?id='+id,'icon-application-form-edit',1);
	}

	function editNews(id){//编辑文章
		addTab('新闻管理','../a_news/news/index.html?id='+id,'icon-chart-organisation',1);
	}

	function deleNews(id){//删除文章
		
		$.ajax({
			type:"delete",
			url:"http://localhost:3000/news/data/"+ id
		}).then(function(res){		
			$('#news-datagrid').datagrid('reload');
		});
		
	}



	function addTab(title, href, iconCls, iframe){//切换界面函数
		var tabPanel =parent.$('#wu-tabs');
		if(!tabPanel.tabs('exists',title)){
			var content = '<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>';
			if(iframe){
				tabPanel.tabs('add',{
					title:title,
					content:content,
					iconCls:iconCls,
					fit:true,
					cls:'pd3',
					closable:true
				});
			}
			else{
				tabPanel.tabs('add',{
					title:title,
					href:href,
					iconCls:iconCls,
					fit:true,
					cls:'pd3',
					closable:true
				});
			}
		}
		else
		{
			tabPanel.tabs('select',title);
			var current_tab=tabPanel.tabs('getSelected');
			tabPanel.tabs('update',{
				tab:current_tab,
				options:{
					content:'<iframe scrolling="auto" frameborder="0"  src="'+href+'" style="width:100%;height:100%;"></iframe>'
				}
			});
		}
	}
</script>
</body>
</html>